package com.zhixinhuixue.zsyte.xxx.ui.activity

import android.os.Bundle
import android.widget.CompoundButton
import androidx.lifecycle.Observer
import me.hgj.mvvmhelper.ext.getStringExt
import com.zhixinhuixue.zsyte.xxx.R
import com.zhixinhuixue.zsyte.xxx.app.App
import com.zhixinhuixue.zsyte.xxx.app.api.NetUrl
import com.zhixinhuixue.zsyte.xxx.app.base.BaseActivity
import com.zhixinhuixue.zsyte.xxx.app.ext.initBack
import com.zhixinhuixue.zsyte.xxx.databinding.ActivityLoginBinding
import com.zhixinhuixue.zsyte.xxx.databinding.ActivityPasswprdBinding
import com.zhixinhuixue.zsyte.xxx.ui.viewmodel.LoginViewModel
import com.zhixinhuixue.zsyte.xxx.ui.viewmodel.PasswordViewModel
import me.hgj.mvvmhelper.ext.logE
import me.hgj.mvvmhelper.ext.showDialogMessage
import me.hgj.mvvmhelper.net.LoadStatusEntity
import me.hgj.mvvmhelper.net.LoadingDialogEntity
import me.hgj.mvvmhelper.util.SPUtil

/**
 * 作者　: hegaojian
 * 时间　: 2020/11/18
 * 描述　: 虽然在Activity代码少了，但是DataBinding 不太好用
 */
class PasswordActivity : BaseActivity<PasswordViewModel, ActivityPasswprdBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        //初始化toolbar
        mToolbar.initBack(getStringExt(R.string.edit_password)) {
            finish()
        }
        mDataBind.viewModel = mViewModel
        mDataBind.click = LoginClickProxy()
    }

    /**
     * 请求成功
     */
    override fun onRequestSuccess() {
        //监听登录结果
        mViewModel.passwordData.observe(this, Observer {
            App.instance.restartApplication()
        })
    }

    /**
     * 请求失败
     * @param loadStatus LoadStatusEntity
     */
    override fun onRequestError(loadStatus: LoadStatusEntity) {
        when (loadStatus.requestCode) {
            NetUrl.PASSWORD -> {
                showDialogMessage(loadStatus.errorMessage)
            }
        }
    }

    inner class LoginClickProxy {

        fun clear() {
            mViewModel.userName.set("")
        }

        //登录
        fun login() {
            var local: String = SPUtil.get(this@PasswordActivity, SPUtil.PASSWORD, "") as String
            var input: String = mViewModel.oldPassword.get()

            when {
                mViewModel.oldPassword.get().isEmpty() -> showDialogMessage("密码不能为空")
                mViewModel.newPassword.get().isEmpty() -> showDialogMessage("新密码不能为空")
                mViewModel.newRePassword.get().isEmpty() -> showDialogMessage("重复新密码不能为空")
                local != input -> showDialogMessage("原始密码输入错误")


                mViewModel.newPassword.get() != mViewModel.newRePassword.get() -> showDialogMessage(
                    "两次密码输入不一致"
                )
                else -> mViewModel.password(mViewModel.oldPassword.get(), mViewModel.newPassword.get())
            }
        }

        var onOldCheckedChangeListener =
            CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
                mViewModel.isShowOldPwd.set(isChecked)
            }
        var onNewCheckedChangeListener =
            CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
                mViewModel.isShowNewPwd.set(isChecked)
            }
        var onReNewCheckedChangeListener =
            CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
                mViewModel.isShowReNewPwd.set(isChecked)
            }
    }

//    override fun showCustomLoading(setting: LoadingDialogEntity) {
//        if(setting.requestCode== NetUrl.LOGIN){
//            //可以根据不同的code 做不同的loading处理
//            showLoadingUi()
//        }
//    }
//
//    override fun dismissCustomLoading(setting: LoadingDialogEntity) {
//        if(setting.requestCode==NetUrl.LOGIN){
//            //可以根据不同的code 做不同的loading处理
//            showSuccessUi()
//        }
//    }

}